import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

Vue.use(VueRouter)
// import Home from '@/views/home/Home.vue'
const Home = () => import('@/views/home/Home.vue')
const Cate = () => import('@/views/cate/Cate.vue')
const Cart = () => import('@/views/cart/Cart.vue')
const User = () => import('@/views/user/User.vue')
const Login = () => import('@/views/user/Login.vue')
const Regist = () => import('@/views/user/Regist.vue')

const Product = () => import('@/views/Product.vue')
const Address = () => import('@/views/user/useraddr/Address.vue')
const Addaddr = () => import('@/views/user/useraddr/Addaddress.vue')
const Editaddr = () => import('@/views/user/useraddr/Editaddress.vue')

const router = new VueRouter({
    mode: 'hash',
    routes: [
        { path: '/', components: { alive: Home } },
        { path: '/cate', component: Cate },
        { path: '/cart', component: Cart, meta: { isAuth: true } },
        { path: '/user', component: User, meta: { isAuth: true } },
        { path: '/user/addr', component: Address },
        { path: '/user/addaddr', component: Addaddr },
        { path: '/user/editaddr', component: Editaddr },
        { path: '/login', component: Login },
        { path: '/regist', component: Regist },
        { path: '/product/:id', component: Product },
    ]
})

export default router

router.beforeEach((to, from, next) => {
    let { isAuth } = to.meta
    let token = store.state.user.token
    if (isAuth) {
        if (token) {
            next()
        } else {
            router.push('/login')
        }
    }
    next()
})